//
//  KLRevicePhoneViewController.m
//  KISSLOCK
//
//  Created by 陈宾鹏 on 16/5/4.
//  Copyright © 2016年 陈宾鹏. All rights reserved.
//

#import "KLRevicePhoneViewController.h"
#import "KLHttpAPI.h"
#import "JRToast.h"
#import "KLHelp.h"

@interface KLRevicePhoneViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *oldPassword;
@property (weak, nonatomic) IBOutlet UITextField *nowPassword;
@property (weak, nonatomic) IBOutlet UITextField *nowPasswordAgain;

@property (weak, nonatomic) IBOutlet UIButton *submitBtn;
@end

@implementation KLRevicePhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化导航栏
    [self naveSet];
    
    self.oldPassword.delegate = self;
    self.nowPassword.delegate = self;
    self.nowPasswordAgain.delegate = self;
}

- (IBAction)submit:(UIButton *)sender {
    [self.view endEditing:YES];
    if(self.oldPassword.text.length == 0 || self.nowPassword.text.length == 0 || self.nowPasswordAgain.text.length == 0){
        [JRToast showWithText:@"请输入完整信息"];
        return ;
    }else if(![self.nowPassword.text isEqualToString:self.nowPasswordAgain.text]){
        [JRToast showWithText:@"两次新密码输入不一致"];
        return ;
    }else if(self.nowPassword.text.length < 6){
        [JRToast showWithText:@"新密码不能低于6位"];
        return ;
    }
    
    [KLHelp showHUDAddedTo:self.view animated:YES];
    [KLHttpAPI requestChangePasswordToken:[[NSUserDefaults standardUserDefaults] objectForKey:@"token"] keytype:@"1" oldPassword:self.oldPassword.text newPassword:self.nowPassword.text completionHandler:^(NSString *responseString) {
        [KLHelp hideAllHUDsForView:self.view animated:YES];
        NSLog(@"修改密码:%@",responseString);
        NSDictionary *dic = [KLHelp dictionaryWithJsonString:responseString];
        NSString *successStr = [dic objectForKey:@"success"];
        if(successStr.integerValue == 1){
            [[NSUserDefaults standardUserDefaults] setObject:self.nowPassword.text forKey:@"password"];
            [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"isRememberPassword"];
            //销毁present出来的控制器
//            [self dismissViewControllerAnimated:NO completion:nil];
            //退回到登录界面 (发送通知)
            [[NSNotificationCenter defaultCenter]postNotificationName:@"changePassword" object:nil];
        }else{
            [JRToast showWithText:[dic objectForKey:@"msg"]];
        }
    } errorHandler:^(NSString *error) {
        [KLHelp hideAllHUDsForView:self.view animated:YES];
    }];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

#pragma mark - 初始化导航栏
-(void)naveSet{
    self.title = @"修改密码";
    [KLHelp navSet:self doBackHandeler:^(UIButton *backButton) {
        [backButton addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    }];
}-(void)doBack:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
